import time
import threading
from mcp_api.server import start_mcp_server
from mcp_api.client import main_client
import sys
import datetime  # 新增：用datetime处理毫秒

# 修正日志函数：用datetime替代time，支持%f微秒格式
def log_step(step):
    # 生成带毫秒的时间戳（截取微秒前3位即毫秒）
    timestamp = datetime.datetime.now().strftime('%H:%M:%S.%f')[:-3]
    print(f"[SEARCH.PY] {timestamp} | {step}")

def search_content(query):

    # 后续代码不变...
    # if len(sys.argv) < 2:
    #     log_step("错误：未收到查询词参数，退出程序")
    #     print("缺少查询词参数")
    #     sys.exit(1)
    # query = sys.argv[1]
    log_step(f"成功获取查询词：{query}")
    server_thread = threading.Thread(target=start_mcp_server, daemon=True)
    server_thread.start()
    log_step("服务器线程已启动")

    time.sleep(10)
    log_step("等待服务器启动完成")

    try:
        result = main_client(query)
        log_step("main_client执行完成")
    except Exception as e:
        result = "123456"
        log_step(f"main_client执行异常：{str(e)}")
    return result

# search_content("喜欢")